//
//  UIBarButtonItem+Extension.swift
//  WeiBoPre19
//
//  Created by EnjoySR on 16/8/28.
//  Copyright © 2016年 传智播客.黑马程序员. All rights reserved.
//

import UIKit

// item 上的文字颜色，高亮颜色，字体大小
// 导航栏上左右的文字一般都会统一，所以可以定义成常量
private let ItemNormalColor = UIColor(red: 80/255, green: 80/255, blue: 80/255, alpha: 1)
private let ItemHighlightedColor = UIColor.orange
private let ItemFontSize: CGFloat = 14

extension UIBarButtonItem {
    
    convenience init(imgName: String? = nil, title: String? = nil, target: Any?, action: Selector) {
        self.init()
        
        let button = UIButton()
        button.addTarget(target, action: action, for: .touchUpInside)
        
        // 如果有图片，设置图片
        if let img = imgName {
            button.setImage(UIImage(named: img), for: UIControlState.normal)
            button.setImage(UIImage(named: "\(img)_highlighted"), for: UIControlState.highlighted)
        }
        
        // 如果有文字，设置文字
        if let t = title {
            button.setTitle(t, for: UIControlState.normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: ItemFontSize)
            button.setTitleColor(UIColor(red: 80/255, green: 80/255, blue: 80/255, alpha: 1), for: .normal)
            button.setTitleColor(UIColor.orange, for: .highlighted)
        }
        button.sizeToFit()
        
        customView = button
    }
    
}
